// @Author EthanScriptOn
// @Desc
package controller

import (
	"gitee.com/fatzeng/srf_switch_basic_components/switch_log"
	"gitee.com/fatzeng/srf_switch_dynamic_logic_director/middleware"
	"gitee.com/fatzeng/srf_switch_dynamic_logic_director/request"
	"gitee.com/fatzeng/srf_switch_dynamic_logic_director/response"
	"gitee.com/fatzeng/srf_switch_dynamic_logic_director/service"
	"github.com/gin-gonic/gin"
)

type AuthController struct{}

func (a *AuthController) Router(router *gin.Engine) {
	entryWithoutAuth := router.Group("/switch_control/v1/auth")
	entryWithoutAuth.POST("/login", a.login)
	entryWithoutAuth.POST("/logout", a.logout)
	entryAuth := router.Group("/switch_control/v1/auth", middleware.IdentityVerification())
	entryAuth.POST("/userInfo", a.userDetail)
}

func (a *AuthController) userDetail(ctx *gin.Context) {
	info, err := service.GetUserDetail(ctx)
	if err != nil {
		switch_log.Logger().Error("AuthController/userDetail", err)
		ctx.JSON(200, response.GenerateCurrencyResponse(nil, response.Fail))
		return
	}
	ctx.JSON(200, response.GenerateCurrencyResponse(info, response.Fail))
}

func (a *AuthController) login(ctx *gin.Context) {
	params := new(request.LoginRequest)
	err := ctx.ShouldBind(params)
	if err != nil {
		switch_log.Logger().Error("AuthController/userDetail", err)
		ctx.JSON(200, response.GenerateCurrencyResponse(nil, response.Fail))
		return
	}
	token, err := service.Login(params)
	if err != nil {
		switch_log.Logger().Error("AuthController/userDetail", err)
		ctx.JSON(200, response.GenerateCurrencyResponse(nil, response.Fail))
		return
	}
	ctx.JSON(200, response.GenerateCurrencyResponse(token, response.Success))
}

func (a *AuthController) logout(ctx *gin.Context) {
	//nothing to do
	ctx.JSON(200, response.GenerateCurrencyResponse(nil, response.Success))
}
